package com.lzy.cloud.goods.facade;

import com.lzy.cloud.goods.dtos.CarConfigDto;
import com.lzy.cloud.goods.dtos.CarParamAppDto;
import com.lzy.cloud.goods.dtos.CarParamDto;
import com.lzy.cloud.goods.dtos.ParamDto;
import com.lzy.cloud.goods.service.CarParamService;
import com.lzy.cloud.goods.service.ParamService;
import com.lzy.framework.core.ensure.Ensure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 基本参数
 *
 * @author lc
 * @version 2018/7/30
 */
@Component
public class ParamFacade {
    @Autowired
    private ParamService paramService;

    @Autowired
    private CarParamService carParamService;

    /**
     * 删除平台端的基本参数
     * @param ids
     * @return
     */
    public Boolean deleteParams(String ids) {
        String[] idx = ids.trim().split(",");
        for (String id:idx) {
            //先判断这个编号的是否被占用
            ParamDto paramDto = paramService.selectById(Long.valueOf(id));
            CarParamDto carParamDto = new CarParamDto();
            carParamDto.setParamId(Long.valueOf(id));
            List list = carParamService.selectList(carParamDto);
            Ensure.that(list.size()).isNotGt(0, "00060006");
            paramService.delById(Long.valueOf(id));
        }
        return true;
    }

    /**
     * 查询核心参数值
     * @return
     */
    public List<CarParamDto> selectCore(String id) {
        ParamDto paramDto = new ParamDto();
        paramDto.setMark(1);
        //得到所有的核心参数
        List<ParamDto> paramDtos = paramService.selectList(paramDto);
        List<CarParamDto> carParamDtos = new ArrayList<CarParamDto>();
        //查询核心参数值
        for (ParamDto dto:paramDtos) {
            CarParamDto carParamDto = new CarParamDto();
            carParamDto.setParamId(dto.getId());
            carParamDto.setShopId(Long.valueOf(id));
            //若出现同一商户出现多个同一参数值取第一个
            List<CarParamDto> carParam = carParamService.selectCoreValue(carParamDto);
            if (carParam.size() != 0){
                carParam.get(0).setParamDto(dto);
                carParamDtos.add(carParam.get(0));
            }
        }
        return carParamDtos;
    }

    /**
     * 查询核心参数值
     * @return
     */
    public List<CarParamAppDto> findCore(Long sourceId) {
       return carParamService.findCore(sourceId);
    }
}
